package com.example.demo.service;

import com.yy.distributed.spring.annotation.DistributedLock;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * Write your description here...
 *
 * @author dinghuan
 * @date 2018/7/20
 */
@Service
public class TestJediLockService {
    public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");

    Random random = new Random();

    @Scheduled(fixedRate = 1000)
    @DistributedLock(tryLock = true, leaseTime = 3, release = false)
    public void executeTest() {
        System.out.println(format.format(new Date()) + " Thread:" + Thread.currentThread().getId() + " get lock..");

        int nextInt = random.nextInt(1000);
        try {
            Thread.sleep(nextInt + 500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
